<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Modules\Blog\Service;


use App\Models\Common\Category;
use Illuminate\Http\Request;

class CategoryService
{

    use Service;

    /**
     * 获取分类
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function CategoryIndexService( $request )
    {
        switch ( $request->input('status') ) {
            case 'all':
                $where    = function ( $query ) use ( $request ) {
                    $query->where('status', 1);
                    $query->where('static', 1);
                    $query->where('is_show', 1);
                    $query->where('parent_id', 0);
                };
                $with     = [
                    'children' => function ( $query ) {
                        $query->select('id', 'parent_id', 'name', 'sort_order', 'is_show', 'status');
                        $query->where('status', 1);
                        $query->where('is_show', 1);
                    },
                ];
                $select   = [ 'id', 'parent_id', 'name', 'sort_order', 'is_show', 'status' ];
                $category = Category::GetModel($where, $with, $select);
                break;
            case 'parent':
                $where    = function ( $query ) use ( $request ) {
                    $query->where('status', 1);
                    $query->where('is_show', 1);
                    $query->where('static', $request->input('static', 1));
                    $query->where('parent_id', $request->input('parent_id', 0));
                };
                $select   = [ 'id', 'parent_id', 'name', 'sort_order', 'is_show', 'status' ];
                $category = Category::GetModel($where, [], $select);
                break;
            default:
                $where                  = function ( $query ) use ( $request ) {
                    $query->where('status', 1);
                    $query->where('is_show', 1);
                    $query->where('static', 1);
                    $query->where('parent_id', 0);
                };
                $where1                 = function ( $query ) use ( $request ) {
                    $query->where('status', 1);
                    $query->where('is_show', 1);
                    $query->where('static', 1);
                    $query->where('parent_id', '!=', 0);
                };
                $select                 = [ 'id', 'parent_id', 'name', 'sort_order', 'is_show', 'status' ];
                $category[ 'category' ] = Category::GetModel($where, [], $select);
                $category[ 'parent' ]   = Category::GetModel($where1, [], $select);
                break;
        }
        return data($category);
    }
}
